<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单总列表'),
	
);
$return['status']=array('0'=>'未支付','1'=>'已支付','2'=>'已确认','3'=>'已取消','4'=>'已完成','5'=>'申请退款','6'=>'已退款','7'=>'拒绝退款');
foreach($return['status'] as $key=>$value){
	$tabs[]=array('op'=>'list','title'=>$value,'query'=>'status='.$key);
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if($_RQ['is_day']==1){
		$params['day']=date('Y-m-d');
		$return['params']['is_day']=1;
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_qiuyuyue_server_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=model('core/member')->detail($item['mid'],array('nickname','mobile'));
			$item['worker_name']=pdo_getcolumn('jy_qiuyuyue_store_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['serve_title']=pdo_getcolumn('jy_qiuyuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['username']=$item['username']?:$user['nickname'];
			$item['user_mobile']=$item['mobile']?:$user['mobile'];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$item['day'],'serveid'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			if($onetime['serve_time']){
				$serve_times=$onetime['serve_time'];
			}
			$serve_time=json_decode($serve_times,true);
			$timeids=str_array($item['timeid']);
			$serve_times='';
			foreach($timeids as $tv){
				if(intval($tv)){
					$timekey=$tv;
				}else{
					$timekey=substr($tv, 0, -1);
				}
				$fee=$serve_time[intval($timekey)]['fee']?:$serve['price'];
				if($item['is_ban']){
					$fee=50*$fee/100;
				}
				$serve_times.=$item['day'].' ';
				$serve_times.=$serve_time[intval($timekey)]['times']['start'].'~'.$serve_time[intval($timekey)]['times']['end'];
				$serve_times.=$item['is_ban']?'半场'.$tv:'全场';
				$serve_times.='、';
			}
			if($item['serve_time']){
				$item['serve_times']=json_decode($item['serve_time'],true);
			}else{
				$item['serve_times']=$serve_times;
			}
			$item['users']=json_decode($item['users'],true);
			$item['usersdesc']='';
			foreach($item['users'] as $uv){
				$item['usersdesc'] .='姓名:'.$uv['name'].'，身份编号:'.$uv['code'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'store_id','title'=>'场馆','type'=>'select','options'=>'stores'),
			array('name'=>'worker_name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'username','title'=>'用户姓名','type'=>'string'),
			array('name'=>'user_mobile','title'=>'用户电话','type'=>'string'),
			array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
			array('name'=>'usersdesc','title'=>'附加预约人','type'=>'string'),
			array('name'=>'package','title'=>'套餐','type'=>'string'),
			array('name'=>'serve_times','title'=>'服务时间','type'=>'string'),
			array('name'=>'pay','title'=>'金额','type'=>'string'),
			array('name'=>'is_sign1','title'=>'场地签到','type'=>'switch','set'=>'set'),
			array('name'=>'is_sign2','title'=>'客户签到','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'备注信息'),
		)),
		'search'=>array(
			array('name'=>'is_day','width'=>15,'title'=>'选择服务时间','type'=>'radio','options'=>'is_day'),
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','width'=>15,'title'=>'员工姓名','type'=>'string'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	$return['stores']=pdo_getall('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['is_sign']=$this->is_sign;
	$return['is_day']=array('全部订单','今日服务');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
	}
	if($_RQ['is_day']==1){
		$params['day']=date('Y-m-d');
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$order=pdo_getall('jy_qiuyuyue_server_order',$params,'','','id desc');
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	foreach($order as &$item){
			$user=model('core/member')->detail($item['mid'],array('nickname','mobile'));
			$item['worker_name']=pdo_getcolumn('jy_qiuyuyue_store_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['serve_title']=pdo_getcolumn('jy_qiuyuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['username']=$item['username']?:$user['nickname'];
			$item['user_mobile']=$item['mobile']?:$user['mobile'];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$item['day'],'serveid'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			if($onetime['serve_time']){
				$serve_times=$onetime['serve_time'];
			}
			$serve_time=json_decode($serve_times,true);
			$timeids=str_array($item['timeid']);
			$serve_times='';
			foreach($timeids as $tv){
				if(intval($tv)){
					$timekey=$tv;
				}else{
					$timekey=substr($tv, 0, -1);
				}
				$fee=$serve_time[intval($timekey)]['fee']?:$serve['price'];
				if($item['is_ban']){
					$fee=50*$fee/100;
				}
				$serve_times.=$item['day'].' ';
				$serve_timesstart=$serve_time[intval($timekey)]['times']['start'];
				$serve_timesend=$serve_time[intval($timekey)]['times']['end'];
			}
			$item['serve_times1']=$serve_times.' '.$serve_timesstart;
			$item['serve_times2']=$serve_times.' '.$serve_timesend;
			$item['store_title']=pdo_getcolumn('jy_qiuyuyue_store',array('id'=>$item['store_id']),'title');
			$item['is_sign1']=$this->is_sign[$item['is_sign1']];
			$item['is_sign2']=$this->is_sign[$item['is_sign2']];
			$item['status']=$this->order_status[$item['status']];
	}
	$header=array(
		'store_title'=>'场馆',
		'username'=>'客户昵称',
		'user_mobile'=>'客户手机号',
		'is_sign1'=>'场地签到',
		'is_sign2'=>'客户签到（可由场地代签）',
		'pay'=>'价格',
		'serve_times1'=>'开始时间',
		'serve_times2'=>'结束时间',
		'status'=>'状态',
		'remark'=>'备注',
	);
	cfc('tools')->export2excel($header,$order,'订单列表');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['is_sign1'])){
		$update['is_sign1']=intval($post['is_sign1']);
	}
	if(isset($post['is_sign2'])){
		$update['is_sign2']=intval($post['is_sign2']);
	}
	if($update){
		pdo_update('jy_qiuyuyue_server_order',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='look'){
	$item=$this->get('server_order',array('id'=>intval($_RQ['id'])),array('id','remark'));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'备注信息',
		'look'=>array(
			array('type'=>'text','name'=>'remark','title'=>'备注信息'),
		),
	);
	exi($return);
}
//确认订单操作
if($_RQ['op']=='confirm'){
	if($_RQ['id']){	
		pdo_update('jy_qiuyuyue_server_order',array('status'=>'2'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('确认订单成功！');
}
//取消订单操作
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
	
		pdo_update('jy_qiuyuyue_server_order',array('status'=>'3'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('取消订单成功！');
}
//同意退款
if($_RQ['op']=='agree'){
	if($_RQ['id']){
	
		pdo_update('jy_qiuyuyue_server_order',array('status'=>'6'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已同意退款申请！');
}
//拒绝退款操作
if($_RQ['op']=='disagree'){
	if($_RQ['id']){
	
		pdo_update('jy_qiuyuyue_server_order',array('status'=>'7'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已拒绝退款申请！');
}